using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ShivamEMR.Model.Domain.PharmacyDomain;
using ShivamEMR.Model.DataInterfaces.PharmacyDataInterf;
using ShivamEMR.Model;
//using ShivamEMR.Web.Areas.Pharmacy.Models;

namespace ShivamEMR.Web.Areas.Pharmacy.Controllers
{   
    public class DrugController : Controller
    {
		private readonly IDrugBrandRepository drugbrandRepository;
		private readonly IDrugGenericRepository druggenericRepository;
		private readonly IDosageRepository dosageRepository;
		private readonly IDrugRepository drugRepository;

		// If you are using Dependency Injection, you can delete the following constructor
        public DrugController() 
        {
            drugbrandRepository = Registry.RepositoryFactory.GetDrugBrandRepository();
            druggenericRepository = Registry.RepositoryFactory.GetDrugGenericRepository();
            dosageRepository = Registry.RepositoryFactory.GetDosageRepository();
            drugRepository = Registry.RepositoryFactory.GetDrugRepository();
        }

        public DrugController(IDrugBrandRepository drugbrandRepository, IDrugGenericRepository druggenericRepository, IDosageRepository dosageRepository, IDrugRepository drugRepository)
        {
			this.drugbrandRepository = drugbrandRepository;
			this.druggenericRepository = druggenericRepository;
			this.dosageRepository = dosageRepository;
			this.drugRepository = drugRepository;
        }

        //
        // GET: /Drug/

        public ViewResult Index()
        {
         //   return View(drugRepository.AllIncluding(drug => drug.DrugBrand, drug => drug.DrugGeneric, drug => drug.Dose, drug => drug.Medications));
            return View(drugRepository.All);
        }

        //
        // GET: /Drug/Details/5

        public ViewResult Details(System.Guid id)
        {
            return View(drugRepository.Find(id));
        }

        //
        // GET: /Drug/Create

        public ActionResult Create()
        {
			ViewBag.PossibleDrugBrands = drugbrandRepository.All;
			ViewBag.PossibleDrugGenerics = druggenericRepository.All;
			ViewBag.PossibleDoses = dosageRepository.All;
            return View();
        } 

        //
        // POST: /Drug/Create

        [HttpPost]
        public ActionResult Create(Drug drug)
        {
            if (ModelState.IsValid) {
                drugRepository.InsertOrUpdate(drug);
                drugRepository.Save();
                return RedirectToAction("Index");
            } else {
				ViewBag.PossibleDrugBrands = drugbrandRepository.All;
				ViewBag.PossibleDrugGenerics = druggenericRepository.All;
				ViewBag.PossibleDoses = dosageRepository.All;
				return View();
			}
        }
        
        //
        // GET: /Drug/Edit/5
 
        public ActionResult Edit(System.Guid id)
        {
			ViewBag.PossibleDrugBrands = drugbrandRepository.All;
			ViewBag.PossibleDrugGenerics = druggenericRepository.All;
			ViewBag.PossibleDoses = dosageRepository.All;
             return View(drugRepository.Find(id));
        }

        //
        // POST: /Drug/Edit/5

        [HttpPost]
        public ActionResult Edit(Drug drug)
        {
            if (ModelState.IsValid) {
                drugRepository.InsertOrUpdate(drug);
                drugRepository.Save();
                return RedirectToAction("Index");
            } else {
				ViewBag.PossibleDrugBrands = drugbrandRepository.All;
				ViewBag.PossibleDrugGenerics = druggenericRepository.All;
				ViewBag.PossibleDoses = dosageRepository.All;
				return View();
			}
        }

        //
        // GET: /Drug/Delete/5
 
        public ActionResult Delete(System.Guid id)
        {
            return View(drugRepository.Find(id));
        }

        //
        // POST: /Drug/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(System.Guid id)
        {
            var drug = drugRepository.Find(id);
            drugRepository.Delete(drug);
            drugRepository.Save();

            return RedirectToAction("Index");
        }
    }
}

